/*  header-comment
/*  file   : /Users/jessica/react-hrm-h5/src/modules/hrCenter/index
/*  author : Allan
/*  date   : 2017-6-4 18:24:50
/*  last   : 2017-11-2 19:14:26
*/
import React, { Component } from "react";
import { bindActionCreators } from "redux";
import propTypes from 'prop-types';
import { connect } from "react-redux";
import { push, replace } from 'react-router-redux'
import FooterBar from "./footerBar/";
import { set,get } from "../../utils/db";
import { checkPermissionV2, fetchHrCenterInfo } from "../../actions/hrCenter/";
import { getCacheData } from "../../utils/storeCache.js";
import urlParams from "../../utils/urlParams";
// utils
import { replaceHash } from "../../utils/browser.js";


import {
  fetchHrCenterInfoV2,
} from "../../actions/hrCenter/";

//corpId
const corpId = urlParams.getUrlSearchParam("corpId");

const mapStateToProps = state => ({
  hrCenter:state.hrCenter,
  footerBar: state.FOOTBAR_INDEX
});
const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators(
    {
      dispatch,
      checkPermissionV2,
      fetchHrCenterInfo,
    },
    dispatch
  )
});

@connect(mapStateToProps, mapDispatchToProps)
export default class HrCenter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      ready: false,
      isAdmin: false,
      userId: '',
      hasPermission:false,
      hasWeekly: false
    };
  }

  async componentDidMount() {
    this.props.actions.checkPermissionV2().then(res => {
      if (res.success) {
        set("userId", res.result.userId);
        set("currentUserId", res.result.userId);
        this.setState({
          ready: true
        });
        if (res.result.hasPermission) {
          this.setState({
            isAdmin: res.result.admin,
            userId: res.result.userId,
            hasPermission: res.result.hasPermission,
            hasWeekly: res.result.hasWeekly
          })
          if (res.result.admin && !res.result.alreadyInited) {
            this.props.router.replace("/initialize");
          } else if (window.location.hash.indexOf('employeeAnalyze') < 0){
            this.props.router.replace("/hrcenter/manage");
          }
          return;
        }

        this.props.router.replace("/hrcenter/selfHelp");

        // history.replaceState("", "", "#hrcenter/serviceCenter");
      }
    });
  }

  render() {
    const { hasPermission,hasWeekly, userId, ready } = this.state;
    if (!ready) {
      return null;
    }
    //是否显示底部菜单
    const { show = true } = this.props.footerBar;
    return (
      <div>
        {ready && (
          <div>
            {this.props.children}
            <FooterBar show={show} userId={userId} hasWeekly={hasWeekly} hasPermission={hasPermission} />
          </div>
        )}
      </div>
    );
  }
}

HrCenter.propTypes = {
  hrCenter: propTypes.shape({
    hasWeekly: propTypes.bool
  })
}